package com.luotao.rpc.web;

import com.luotao.rpc.provider.UserService;
import com.luotao.service.CustService;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: luotao
 * Date: 12-5-28
 * Time: 23:06
 */
public class UserServlet extends HttpServlet {

    private UserService userService;

    private CustService custService;

    @Autowired
    public void setCustService(CustService custService) {
        this.custService = custService;
        System.out.println("this.custService = " + this.custService);
        System.out.println("hashCode() = " + hashCode());
    }

    @Autowired
    public void setUserService(UserService userService) {
        System.out.println("this.custService = " + this.custService);
        System.out.println("hashCode() = " + hashCode());
        this.userService = userService;
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        ApplicationContext app = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext());
//       xapp.start();
       app.getAutowireCapableBeanFactory().autowireBean(this);
    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("userService = " + userService);
        System.out.println("hashCode() = " + hashCode());
        System.out.println("custService = " + this.custService);
   //     System.out.println("userService = " + userService.sayHello("ehllo"));
    }
}
